package com.zb.session;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import com.zb.common.po.config.ConfigInfo;
import com.zb.common.utils.Constants;
import com.zb.common.utils.CookieUtil;
import com.zb.common.utils.SpringHelper;

/**
 * HttpSession包装类。
 * 
 */
@SuppressWarnings("deprecation")
public class HttpSessionWrapper implements HttpSession {

    private HttpSession session;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private String sid = "";
    private Map<String, Object> map = null;

    private static ConfigInfo configInfo = (ConfigInfo) SpringHelper
            .getBean("configInfo");
    // session有效期，30秒
    public static final int EXPIRY = configInfo.getExpiry();// 60 *30;

    public HttpSessionWrapper(HttpSession session) {
        this.session = session;
    }

    public HttpSessionWrapper(String sid, HttpSession session) {
        this(session);
        this.sid = sid;
    }

    public HttpSessionWrapper(String sid, HttpSession session,
            HttpServletRequest request, HttpServletResponse response) {
        this(sid, session);
        this.request = request;
        this.response = response;
    }

    private Map<String, Object> getMap() {
        if (this.map == null) {
            this.map = SessionService.getInstance().getSession(sid);
        }
        return this.map;
    }

    public String getId() {
        return this.sid;
    }

    public Object getAttribute(String name) {
        // 获取值
        return this.getMap().get(name);
    }

    public void setAttribute(String key, Object value) {
        // 存值
        this.getMap().put(key, value);
        SessionService.getInstance().saveSession(this.sid, this.getMap(),
                EXPIRY);
    }

    @SuppressWarnings("rawtypes")
    public Enumeration getAttributeNames() {
        return (new Enumerator(this.getMap().keySet(), true));
    }

    public void invalidate() {
        this.getMap().clear();
        SessionService.getInstance().removeSession(this.sid);
        // 清除cookie
        CookieUtil.removeCookieValue(request, response, Constants.JSESSIONID);
    }

    public void removeAttribute(String key) {
        this.getMap().remove(key);
        SessionService.getInstance().saveSession(this.sid, this.getMap(),
                EXPIRY);
    }

    public long getCreationTime() {
        return session.getCreationTime();
    }

    public long getLastAccessedTime() {
        return session.getLastAccessedTime();
    }

    public int getMaxInactiveInterval() {
        return session.getMaxInactiveInterval();
    }

    public ServletContext getServletContext() {
        return session.getServletContext();
    }

    public HttpSessionContext getSessionContext() {
        return session.getSessionContext();
    }

    public Object getValue(String arg0) {
        return session.getValue(arg0);
    }

    public String[] getValueNames() {
        return session.getValueNames();
    }

    public boolean isNew() {
        return session.isNew();
    }

    public void putValue(String arg0, Object arg1) {
        session.putValue(arg0, arg1);
    }

    public void removeValue(String arg0) {
        session.removeValue(arg0);
    }

    public void setMaxInactiveInterval(int arg0) {
        session.setMaxInactiveInterval(arg0);
    }
}
